package com.glory.cqaospda.view

import android.annotation.SuppressLint
import android.content.Intent
import android.os.*
import android.view.View
import com.glory.cqaospda.R
import com.glory.cqaospda.base.MyApp
import com.glory.cqaospda.viewmodel.InformationInputModel
import com.glory.mvvmnet.base.BaseActivity
import com.glory.mvvmnet.entity.ToastMessageBean
import com.google.gson.JsonObject
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ActivityManager
import kotlinx.android.synthetic.main.activity_informationinput.*
import kotlinx.android.synthetic.main.activity_title.*

class InformationInputActivity() : BaseActivity<InformationInputModel>(), Parcelable {


    var handler: Handler = object : Handler() {
        @SuppressLint("HandlerLeak")
        override fun handleMessage(msg: Message) {
            when(msg.what) {
                0x10 -> {
                    var s: String = msg.obj as String
                    val intent = Intent()
                    intent.putExtra("type", "Error")
                    intent.putExtra("error", s)
                    val clazz = Class.forName("com.glory.cqaospda.view.SkipDialogActivity")
                    intent.setClass(this@InformationInputActivity, clazz)
                    startActivity(intent)
                }
            }
            super.handleMessage(msg)
        }
    }

    constructor(parcel: Parcel) : this() {

    }

    override fun layoutId(): Int {
        return R.layout.activity_informationinput
    }

    override fun initData() {
        btInput.setOnClickListener {
            if (etLotNo.text.toString().equals("")
                || etEquipmentName.text.toString().equals("")
                || etCarrierTape.text.toString().equals("")
                || etCoverTape.text.toString().equals("")
                || etTemperature.text.toString().equals("")){
                viewModel.defUI.toastEvent.value = ToastMessageBean("相关信息不能留空",false)
                return@setOnClickListener
            }
            val map = JsonObject()
            map.addProperty("LotNo",etLotNo.text.toString())
            map.addProperty("EquipmentName",etEquipmentName.text.toString())
            map.addProperty("CarrierTape",etCarrierTape.text.toString())
            map.addProperty("CoverTape",etCoverTape.text.toString())
            map.addProperty("Temperature",etTemperature.text.toString())
            viewModel.defaultPeelingForce(map)
        }

        viewModel.defUI.callObserve.observe(this@InformationInputActivity) {
            viewModel.defUI.toastEvent.value = ToastMessageBean("操作成功",true)
            finish()
        }
    }

    override fun initView(savedInstanceState: Bundle?) {

        FreeApi_ActivityManager.getActivityManager().addActivity(this)
        tvTitle.text = "信息录入"
        val instance = MyApp.instance
        instance!!.handler = handler
        tvBaseSubmit.visibility = View.GONE
    }

    override fun writeToParcel(parcel: Parcel, flags: Int) {

    }

    override fun describeContents(): Int {
        return 0
    }

    companion object CREATOR : Parcelable.Creator<InformationInputActivity> {
        override fun createFromParcel(parcel: Parcel): InformationInputActivity {
            return InformationInputActivity(parcel)
        }

        override fun newArray(size: Int): Array<InformationInputActivity?> {
            return arrayOfNulls(size)
        }
    }
}